package editor
{
	import UIFree.uif_component;
	
	import components.uif_loaderBar;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;

	public class pi_imageLoader extends pi_loader {
		private var _process: Array;
		private var _label: uif_loaderBar;
		private var _updateTimer: Timer;
		public function pi_imageLoader(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:String='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			var inner: Rectangle = getInner();
			_label = new uif_loaderBar(this, inner.x, inner.y, inner.width, 15, 'loaderBar');
			_process = new Array();
			_updateTimer = new Timer(400);
			_updateTimer.addEventListener(TimerEvent.TIMER, doUpdateTimer);
			_updateTimer.start();			
		}
		
		override protected function getTimeout(): int {
			return 30000;
		}
		
		protected function doUpdateTimer(e: Event): void {
			var curPercent: Number = getLoadedPercent();
			_label.percent = curPercent;
			if (curPercent == 100) slideHide();
			else slideShow();
		};
		
		override protected function initEvents(): void {
			app.addEventListener(GDataEvent.REQUESTIMAGE, addRequest);
			app.addEventListener(GDataEvent.RESPONSEIMAGE, decRequest); 
//			app.addEventListener(GDataEvent.IMAGELOADERROR, decRequest);
			app.addEventListener(Event.REMOVED, doRemove);
		}
		
		private function removeLoader(loader: Loader): void {
			if (loader) {
				var index: int = _process.indexOf(loader);
				if (index > -1) _process.splice(index, 1);
			}
		}
		
		private function doRemove(e: Event): void {
			removeLoader(e.target as Loader);
		}
		
		override protected function addRequest(e: Event): void {
			if (e is GDataEvent) {
				var egd: GDataEvent = e as GDataEvent;
				if (egd.data is Loader) {
					_process.push(egd.data);
				}
			}
		}
		
		override protected function decRequest(e: Event): void {
			if (e is GDataEvent) {
//				trashFree();
				var egd: GDataEvent = e as GDataEvent;
				removeLoader(egd.data as Loader);
			}
		}
		
		protected function trashFree(): void {
			var i: int = 0;
			while (i < _process.length) {
				if (!_process[i].parent) _process.splice(i, 1);
				else if (_process[i].contentLoaderInfo.bytesTotal == _process[i].contentLoaderInfo.bytesLoaded) 
					_process.splice(i, 1);
				else i++;
			}
		}
		
		public function getLoadedPercent(): Number {
			var tBytes: int = 0, lBytes: int = 0;
			for (var i:int = 0; i<_process.length; i++) {
				if (_process[i].contentLoaderInfo.bytesTotal > _process[i].contentLoaderInfo.bytesLoaded) {
					tBytes += _process[i].contentLoaderInfo.bytesTotal;
					lBytes += _process[i].contentLoaderInfo.bytesLoaded;
				} else if (_process[i].contentLoaderInfo.bytesTotal == 0) {
					tBytes += 100;
				}
			}
			if (tBytes > 0) return lBytes / tBytes * 100;
			else return 100;
		}
	}
}